﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PeekService;

namespace PeekWebApp.handler
{
    /// <summary>
    /// ListHandler 的摘要说明
    /// </summary>
    public class ListHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string action = context.Request["action"];
            switch (action)
            {
                case "List"://推荐景点
                    string typeId = context.Request["ti"];
                    string regionId = context.Request["ri"];
                    int psize = int.Parse(context.Request["pz"]);
                    int pindex = int.Parse(context.Request["pi"]);
                    int recordCount = 0;
                    List<RecommendModel> list = new ExtendService().GetRecomendList(typeId, regionId, false, pindex, psize, out recordCount);
                    int pageCount = 0;
                    if (recordCount % psize == 0)
                    {
                        pageCount = recordCount / psize;
                    }
                    else
                    {
                        pageCount = recordCount / psize + 1;
                    }
                    context.Response.Write(CommonService.ConvertToJSON(new { pager = new { psize = psize, pindex = pindex, pcount = pageCount }, data = list, typeInfo = new ExtendService().GetTourCount(typeId, regionId) }));
                    break;

                case "More":                    
                    string rid = context.Request["ri"];
                    int count = 0;
                    List<TourismInfoType> typeList = new TourismInfoTypeService().GetAll();
                    List<RecommendModel> listMore = new List<RecommendModel>();
                    List<object> typeInfoList = new List<object>();
                    foreach (TourismInfoType type in typeList)
                    {
                        List<RecommendModel> listTour = new ExtendService().GetRecomendList(type.Code, rid, false, 1, 3, out count);
                        typeInfoList.AddRange(new ExtendService().GetTourCount(type.Code, rid));
                        listMore.AddRange(listTour);
                    }                   
                    context.Response.Write(CommonService.ConvertToJSON(new { data = listMore, typeInfo =typeInfoList  }));

                    break;
                default: break;
            }
           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}